package com.woniuxy.joylifecenter.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniuxy.common.constant.ForbiddenWordsConstant;
import com.woniuxy.common.entity.CenterForbiddenwords;
import com.woniuxy.joylifecenter.exception.CenterException;
import com.woniuxy.joylifecenter.service.ForbiddenWordsService;
import com.woniuxy.joylifecenter.vo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Api(value = "", description = "禁词表审核API")
@Controller
public class ForbiddenWordsController {
    @Autowired
    ForbiddenWordsService forbiddenWordsService;

    /**
     * @return java.lang.Integer
     * @Author ArmWatt
     * @Date 2020/5/11
     * @Param [words]
     * @Description 禁词审核
     **/
    @ApiOperation(value = "/checkWords", notes = "禁词审核")
    @RequestMapping("/checkWords")
    @ResponseBody
    public Integer checkWords(@RequestBody List<String> words) {
        return forbiddenWordsService.checkWords(words);
    }

    @ApiOperation(value = "/batchAddWords", notes = "批量添加禁词")
    @RequestMapping("/batchAddWords")
    @ResponseBody
    public VO batchAddWords(@RequestBody String jsonstr) throws IOException {
        System.out.println("后端接收到的数据:" + jsonstr);

        VO vo = new VO();
        vo.setCode(0);
        vo.setData(null);
        int count = 0;
        vo.setCount(count);
        vo.setMsg("");

        ObjectMapper mapper = new ObjectMapper();
        List<String> orgList = mapper.readValue(jsonstr, List.class);

        List<CenterForbiddenwords> words = new ArrayList<CenterForbiddenwords>();
        System.out.println("前端发送的数据:" + jsonstr);
        System.out.println("json串转对象:" + orgList);
        //将前端提交过来的数据去重
        List<String> list=orgList.stream().distinct().collect(Collectors.toList());
        for (String keyWord : list) {
            if (!keyWord.equals("")) {
                CenterForbiddenwords word = new CenterForbiddenwords();
                word.setCenterAddtime(new Date());
                word.setCenterFibwordid(UUID.randomUUID().toString().replaceAll("-", ""));
                word.setCenterFibwordStatus(new Byte(ForbiddenWordsConstant.PASS.toString()));
                word.setCenterKeyword(keyWord);
                words.add(word);
            }
        }
        System.out.println("将要批处理的对象:" + words);
        List<String> existWords = forbiddenWordsService.batchAddWords(words);
        if (existWords == null) {
            vo.setMsg("批量添加成功");
        } else {
            vo.setMsg("批量添加失败,与下列禁词重复:" + existWords);
            vo.setCount(existWords.size());

        }
        return vo;
    }

    @ApiOperation(value = "/batchDeleteWords", notes = "批量删除禁词")
    @RequestMapping("/batchDeleteWords")
    @ResponseBody
    public VO batchDeleteWords(@RequestBody List<String> words) {
        VO vo = new VO();
        vo.setCode(0);
        vo.setData(null);
        int count = 0;
        System.out.println("前端发送的批量删除数据:" + words);
        count = forbiddenWordsService.batchDeleteWords(words);
        vo.setMsg("批量删除了:");
        vo.setCount(count);
        return vo;
    }

    @ApiOperation(value = "/showWordsByCondition", notes = "多条件模糊查禁词")
    @RequestMapping("/showWordsByCondition")
    @ResponseBody
    public VO showWordsByCondition(@RequestBody HashMap<String, String> pageMap) {
        VO vo = new VO();
        vo.setCode(0);
        vo.setData(null);
        int count = 0;
        System.out.println("前端发送的模糊查询数据:" + pageMap);
        String jsonstr = pageMap.get("jsonstr");
        ObjectMapper mapper = new ObjectMapper();
        CenterForbiddenwords word = new CenterForbiddenwords();
        Map<String, String> map = new HashMap<String, String>();
        try {
            map = (HashMap<String, String>) mapper.readValue(jsonstr, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //前端传过来的日期字符串必须验是否为空串，否则会报类型转换异常
        if (!map.get("centerAddtime").equals("")) {
            try {
                word.setCenterAddtime(DateFormat.getDateTimeInstance().parse(map.get("centerAddtime")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //如果前端传过来的字符串要转换成包装类，必须验是否为空串，再实例化包装类对象，否则会报类型转换异常
        if (!map.get("centerFibwordStatus").equals("")) {
            Byte centerFibwordStatus = new Byte(map.get("centerFibwordStatus"));
            word.setCenterFibwordStatus(centerFibwordStatus);
        }
        word.setCenterKeyword(map.get("centerKeyword"));
        word.setCenterFibwordid(map.get("centerFibwordid"));
        PageHelper.startPage(Integer.parseInt(pageMap.get("page")), Integer.parseInt(pageMap.get("limit")));
        List<CenterForbiddenwords> words = forbiddenWordsService.queryWordsByCondition(word);
        PageInfo pageInfo = new PageInfo(words);
        List<PageInfo> pageInfos = new ArrayList<PageInfo>();
        pageInfos.add(pageInfo);
        vo.setData(pageInfos);
        vo.setMsg("");
        vo.setCount(new Long(pageInfo.getTotal()).intValue());
        return vo;
    }


    @ApiOperation(value = "/editWord", notes = "修改禁词")
    @RequestMapping("/editWord")
    @ResponseBody
    public VO editWord(@RequestBody CenterForbiddenwords word) {
        VO vo = new VO();
        vo.setCode(0);
        vo.setData(null);
        int count = 0;
        vo.setCount(count);
        System.out.println("前端发送的数据:" + word);
        try {
            word.setCenterAddtime(new Date());
            count = forbiddenWordsService.editWord(word);
            vo.setMsg("本次更新了:");
            vo.setCount(count);
        } catch (CenterException e) {
            vo.setMsg(e.getMessage());
        }
        return vo;
    }


}
